package com.junlinpro.utils;

import javax.naming.InvalidNameException;

import org.apache.directory.api.util.Hex;

public class ByteUtil {

    protected final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    public static String mac(final byte[] data) {
        if (data == null || data.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        final char[] out = new char[data.length * 3 - 1];
        int g = 0;
        for (int j = 0; j < 6; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];
            if (g < 5) {
                buffer.append(out[j * 3]);
                buffer.append(out[j * 3 + 1] + ":");
            } else {
                buffer.append(out[j * 3]);
                buffer.append(out[j * 3 + 1]);
            }
            g++;
        }
        return buffer.toString();
    }

    public static String parse(final byte[] data) {
        if (data == null || data.length == 0)
            return "";

        StringBuffer buffer = new StringBuffer();
        final char[] out = new char[data.length * 3 - 1];
        for (int j = 0; j < data.length; j++) {
            int v = data[j] & 0xFF;
            out[j * 3] = HEX_ARRAY[v >>> 4];
            out[j * 3 + 1] = HEX_ARRAY[v & 0x0F];

            buffer.append(" " + out[j * 3]);
            buffer.append(out[j * 3 + 1]);
        }
        return buffer.toString();
    }

    public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase() + " ");
        }
        return sb.toString();
    }

    public static int byteToInt(byte b) {
        return b & 0xFF;
    }

    public static int dataLengthToInt(byte[] b) {
        return b[1] & 0xFF |
                (b[0] & 0xFF) << 8;
    }

    public static int byteArrayToInt(byte[] b) {
        int a = b[3] & 0xFF |
                (b[2] & 0xFF) << 8 |
                (b[1] & 0xFF) << 16 |
                (b[0] & 0xFF) << 24;

        //a = a & 0xff;
        //ByteArrayInputStream in = new ByteArrayInputStream(b);
        return a;
    }

    public static byte[] intToByteArray(int a) {
        return new byte[]{
                (byte) ((a >> 24) & 0xFF),
                (byte) ((a >> 16) & 0xFF),
                (byte) ((a >> 8) & 0xFF),
                (byte) (a & 0xFF)
        };
    }

    public static byte[] toByteArray(short[] src) {

        int count = src.length;
        byte[] dest = new byte[count << 1];
        for (int i = 0; i < count; i++) {
            dest[i * 2] = (byte) (src[i] >> 0);
            dest[i * 2 + 1] = (byte) (src[i] >> 8);
        }

        return dest;
    }

    public static byte[] intTo2ByteArray(int a) {
        return new byte[]{
                (byte) ((a >> 8) & 0xFF),
                (byte) (a & 0xFF)
        };
    }
    
    /**
               *  将Hex String转换为Byte数组
     *
     * @param hexString the hex string
     * @return the byte [ ]
     */
    public static byte[] hexStringToBytes(String hexString) {
//        if (StringUtils.isEmpty(hexString)) {
//            return null;
//        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexString.length(); i++) {
            if (index  > hexString.length() - 1) {
                return byteArray;
            }
            byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
            byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte) (highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }
    
    /*
    public static byte[] hexStringToBytes(String hex)
	{
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
 
		for (int i = 0; i < len; i++)
		{
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result; 
	}
	*/

}

